package com.example.tianhao.httpclient;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by TianHao on 2017/4/7.
 */

public class StreamTools {
    public static String readStream(InputStream is)
    {
        try {
        ByteArrayOutputStream baos=new ByteArrayOutputStream();
        byte[] buffer=new byte[1024];
        int len=-1;
        while(is.read(buffer)!=-1){
            baos.write(buffer,0,len);
        }
        is.close();
        String temptext=new String(baos.toByteArray());
        if(temptext.contains("charest=GBK")) {
            return new String(baos.toByteArray(), "GBK");
        }else
        {
            return new String(baos.toByteArray(), "UTF-8");
        }
    }catch (IOException e){
        e.printStackTrace();
        return  null;
    }
    }
}
